package com.example.currentlimiting.demo.controller;

import com.example.currentlimiting.demo.annotation.RateLimit;
import com.example.currentlimiting.demo.annotation.RequestLimit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
public class TestController {

    /**
     * 1s内只接受3个请求
     * @return
     */
    @RequestLimit(time = 1, limit = 3)
    @GetMapping(value = "/hello1", produces = "application/json;charset=UTF-8")
    public ResponseEntity<String> hello1() {
        return ResponseEntity.ok("1s内只接受3个请求:" + System.currentTimeMillis());
    }

    /**
     * 每秒内只接受3个请求
     * @return
     */
    @RateLimit(limit = 3)
    @GetMapping(value = "/hello2", produces = "application/json;charset=UTF-8")
    public ResponseEntity<String> hello2() {
        return ResponseEntity.ok("每秒内只接受3个请求:" + System.currentTimeMillis());
    }

}
